package com.sbr.tbd.services;

import com.sbr.tbd.dao.ApplicationDao;
import com.sbr.tbd.dao.FeatureDao;
import com.sbr.tbd.model.Application;
import com.sbr.tbd.model.Feature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdministrationService {

	private static final Logger LOG = LoggerFactory.getLogger(AdministrationService.class);
	
	@Resource
	private ApplicationDao applicationDao;
	
	@Resource
	private FeatureDao featureDao;
	
	public void addApplication(Application application) {
		if (applicationDao.isNameAlreadyUsed(application.getName()) != null){
			LOG.warn("le nom {} est déjà utilisé", application.getName());
		}
		
		applicationDao.save(application);
	}

	public void addFeature(Feature feature) {
		if (featureDao.isNameAlreadyUsed(feature.getName()) != null){
			LOG.warn("le nom {} est déjà utilisé", feature.getName());
		}

        // By default, the feature must be linked to all applications
        feature.setApplications(applicationDao.readAll());

		featureDao.save(feature);
	}

    public void delete(Application application) {
       //We must purge the application from all the features
        List<Feature> features = featureDao.readAll();


//        for(Feature feature : features) {
//            feature.setApplications();
//        }


    }

	public void setApplicationDao(ApplicationDao applicationDao) {
		this.applicationDao = applicationDao;
	}

	public void setFeatureDao(FeatureDao featureDao) {
		this.featureDao = featureDao;
	}


}
